﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using 刘颜红销售项目管理.Business.DOM ;
using 刘颜红销售项目管理.Business ;
using XDesigner.ORM;

namespace 刘颜红销售项目管理
{
    public partial class frm项目列表 : DevExpress.XtraEditors.XtraForm
    {
        public frm项目列表()
        {
            InitializeComponent();
            strTitle = this.Text;
        }
        
        private string strTitle = null;
        
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            MyApplicationFramework.Instance.ORMEngine.OpenConnection();

            cboMachineTypes.Items.Clear();
            Array types = MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class产品型号));
            foreach (Class产品型号 type in types)
            {
                if (type.型号 != null)
                {
                    cboMachineTypes.Items.Add(type.型号);
                }
            }

            myUnitList.DataSource = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(
                typeof( Class工作单位 ) ,
                MyApplicationFramework.Instance.ORMEngine.ReadAllInstances( typeof( Class工作单位 )));

            Array units = MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class人员基本信息));
            ORMBindingSource ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(typeof(Class人员基本信息), units);
            myPeopleList.DataSource = ds;

            units = MyApplicationFramework.Instance.ORMEngine.ReadAllInstances(typeof(Class项目基本信息));
            ds = MyApplicationFramework.Instance.ORMEngine.CreateBindingSource(
                typeof(Class项目基本信息),
                units);

            ds.LogItemChange = true;
            ds.Changed += new EventHandler(ds_Changed);
            myGridControl.DataSource = ds;
            MyApplicationFramework.Instance.ORMEngine.CloseConnection();
        }

        void ds_Changed(object sender, EventArgs e)
        {
            this.Text = strTitle + "*";
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Append);
            //DataGridViewRow row = myGridControl myDataGridView.Rows[ myDataGridView.NewRowIndex ] ;
            //row.Selected = true ;
            //myDataGridView.CurrentCell = row.Cells[1];
            //myDataGridView.BeginEdit(false );
        }

        private void frm单位基本信息_Load(object sender, EventArgs e)
        {
           
            btnRefresh_Click(null, null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.INavigatableControl ctl = (DevExpress.XtraEditors.INavigatableControl)myGridControl;
            ctl.DoAction(DevExpress.XtraEditors.NavigatorButtonType.Remove);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource)myGridControl.DataSource;
            if (ds.Modified)
            {
                if (MessageBox.Show(
                    this,
                    "记录已经修改，是否保存?",
                    "系统提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                }
            }
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ORMBindingSource ds = (ORMBindingSource) this.myGridControl.DataSource;
            if (ds.Modified)
            {
                ds.Engine.OpenConnection();
                int result = ds.Update();
                ds.Engine.CloseConnection();
                ds.Engine.Buffer.Clear(typeof(Class项目基本信息));
                this.Text = strTitle;
                MessageBox.Show("更新了 " + result + " 条记录.");
            }
        }
         

        private void myGridView_DoubleClick(object sender, EventArgs e)
        {
            Class项目基本信息 item = ( Class项目基本信息  )myGridView.GetFocusedRow();
            if (item != null)
            {
                using (dlg项目基本信息 dlg = new dlg项目基本信息())
                {
                    dlg.项目基本信息 = item;
                    dlg.ShowDialog(this);
                    //myGridView.RefreshData();
                    //myGridView.RefreshRow(  );
                    //myGridControl.RefreshDataSource();
                }
            }
        } 
    }
}